package com.leo.ibatis.demo.web.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.leo.ibatis.demo.criteria.InventoryCriteria;
import com.leo.ibatis.demo.domain.Inventory;
import com.leo.ibatis.demo.service.InventoryService;

public class InventoryController extends MultiActionController {

	private InventoryService inventoryService;

	public void setInventoryService(InventoryService inventoryService) {
		this.inventoryService = inventoryService;
	}

	public ModelAndView list(HttpServletRequest request, HttpServletResponse response) throws Exception {
		InventoryCriteria inventoryCriteria = new InventoryCriteria();
		bind(request, inventoryCriteria);
		
		List<Inventory> inventoryList = inventoryService.listOnPage(inventoryCriteria);
		
		ModelAndView mav = new ModelAndView("inventory/list");
		mav.addObject("inventoryList", inventoryList);
		mav.addObject("inventoryCriteria", inventoryCriteria);
		
		return mav;
	}

	public ModelAndView detail(HttpServletRequest request, HttpServletResponse response) throws Exception {
		InventoryCriteria inventoryCriteria = new InventoryCriteria();
		bind(request, inventoryCriteria);
			
		String itemid = ServletRequestUtils.getRequiredStringParameter(request, "itemid");

		Inventory inventory = inventoryService.select(itemid);
		
		ModelAndView mav = new ModelAndView("inventory/detail");
		mav.addObject("inventory", inventory);
		mav.addObject("inventoryCriteria", inventoryCriteria);
		
		return mav;
	}

	public ModelAndView delete(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String itemid = ServletRequestUtils.getRequiredStringParameter(request, "itemid");

		inventoryService.delete(itemid);
		
		return new ModelAndView("redirect:inventory.do");
	}

}
